/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.data.objects;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;

import ronp.anim.data.Grip;

/**
 * Represents a line that will be drawn on the screen
 * 
 * @author Daniel Centore
 *
 */
public class RNLine extends RNObject
{
	private static final long serialVersionUID = 1L;

	private Grip start; // The start of the line
	private Grip end; // The end of the line
	private Color color; // Color of the line

	/**
	 * Creates an {@link RNLine}
	 * @param start Start of the line
	 * @param end End of the line
	 * @param color Color to make the line
	 */
	public RNLine(Grip start, Grip end, Color color)
	{
		addGrips(start, end);
		this.start = start;
		this.end = end;
		this.color = color;
	}

	@Override
	public void drawImage(int frame, Graphics2D g2, boolean selected)
	{
		Point p1 = start.getPosition(frame);
		Point p2 = end.getPosition(frame);

		if (p1 == null)
			return;

		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Stroke old = g2.getStroke();
		g2.setStroke(new BasicStroke(2f));

		if (!selected)
			g2.setColor(color);
		else
		{
			g2.setPaint(new GradientPaint(0, 0, color, 1, 1, findOppositeColor(color), true)); // true means to repeat pattern
		}

		g2.drawLine(p1.x, p1.y, p2.x, p2.y);
		g2.setStroke(old);
	}

	/**
	 * Finds the opposite of a {@link Color} on a color wheel.
	 * Uses the algorithm {@code 0xFFFFFF - orig}
	 * @param orig The {@link Color} to find the opposite of
	 * @return The opposite {@link Color}
	 */
	public static Color findOppositeColor(Color orig)
	{
		int color = orig.getRGB();
		int hex = 0xFFFFFF - color;

		return new Color(hex);
	}

	@Override
	public String toString()
	{
		return "Line";
	}

}
